package com.xt.core.dao;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;

import com.xt.core.helper.page.Pager;
import com.xt.core.exception.ObjectCanNotSaveException;

/**
 * Generic DAO (Data Access Object) with common methods to CRUD POJOs.
 *
 * <p>Extend this interface if you want typesafe (no casting necessary) DAO's for your
 * domain objects.
 *
 *
 * @author txiao
 * @version Revision: 1.0, Date: 23.09.2009
 * @param <T> a type variable
 * @param <PK> the primary key for that type
 */
public interface BasicDao<T, PK extends Serializable> {
	
	
	/** the basic functions */
	/**
     * Generic method used to get all objects of a particular type. This
     * is the same as lookup up all rows in a table.
     * @return List of populated objects
     */
    List<T> getAll();

    /**
     * Gets all records without duplicates.
     * <p>Note that if you use this method, it is imperative that your model
     * classes correctly implement the hashcode/equals methods</p>
     * @return List of populated objects
     */
    List<T> getAllDistinct();

    /**
     * Generic method to get an object based on class and identifier. An
     * ObjectRetrievalFailureException Runtime Exception is thrown if
     * nothing is found.
     *
     * @param id the identifier (primary key) of the object to get
     * @return a populated object
     * @see org.springframework.orm.ObjectRetrievalFailureException
     */
    T get(PK id);

    /**
     * Checks for existence of an object of type T using the id arg.
     * @param id the id of the entity
     * @return - true if it exists, false if it doesn't
     */
    boolean exists(PK id);

    /**
     * Generic method to save an object - handles both update and insert.
     * @param object the object to save
     * @return the persisted object
     */
    T save(T object);

    /**
     * Generic method to delete an object based on class and id
     * @param id the identifier (primary key) of the object to remove
     */
    void remove(PK id);

    public void delete(T object);
    
    /**
     * Find a list of records by using a named query
     * @param queryName query name of the named query
     * @param queryParams a map of the query names and the values
     * @return a list of the records found
     */
    List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams);
	
	/*
     * extended functions
     */
	public void saveorupdate(T object) throws ObjectCanNotSaveException;
	
	public int getTotalRows();
	public int getTotalRows(Set<LinkedHashMap<String, Object>> queryParams);
	public long getTotalRows(String queryString);
	
	public List<T> getAll(Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order);
	
	public List<T> getOnePage(int pageSize, int startRow);
	public List<T> getOnePage(Pager pager);
	
	public List<T> getOnePage(int pageSize, int startRow, Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order);
	public List<T> getOnePage(Pager pager, Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order);
		
	public List<T> getOnePage( String queryString, int pageSize, int startRow);
	public List<T> getOnePage( String queryString, Pager pager);
	
}
